﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using MandalaySoft.Common;
using System.Data.SqlClient;
using System.Data;

namespace SIMS.BL
{
    public class CourseDB
    {
        MyDatabase _db = new MyDatabase();

        public DataTable GetCourseList(int SubjectID, out MyError error)
        {
            DataTable _dt = new DataTable("tblCourse");
            SortedList _paramList = new SortedList();
            _paramList.Add("@SubjectID", SubjectID);

            _dt = _db.GetDataTable("sprocCourseSelectList", CommandType.StoredProcedure, _paramList, out error);
            return _dt;
        }

        public int Delete(int CourseID, out MyError error)
        {
            SortedList _paramList = new SortedList();
            _paramList.Add("@CourseID", CourseID);

            return _db.ExecuteNonSql("sprocCourseDeleteSingleItem", CommandType.StoredProcedure, _paramList, out error);
        }

        public Course GetItem(int CourseID)
        {
            Course item = new Course();

            SortedList _paraList = new SortedList();
            _paraList.Add("@CourseID", CourseID);

            MyError _error = new MyError();
            SqlDataReader _reader = _db.GetSqlDataReader("sprocCourseSelectSingleItem", System.Data.CommandType.StoredProcedure, _paraList, out _error);
            item.Error = _error;

            if (_error.ErrorNo == 0 && _reader.Read())
            {
                item = FillData(_reader, item);
            }

            return item;
        }

        public Course Save(Course item)
        {
            try
            {
                SortedList _paraList = new SortedList();
                _paraList.Add("@CourseID", item.CourseID);
                _paraList.Add("@SubjectID", item.SubjectID);
                _paraList.Add("@GradeLevelID", item.GradeLevelID);
                _paraList.Add("@CourseName", item.CourseName);
                _paraList.Add("@ShortName", item.ShortName);
                _paraList.Add("@IsActive", item.IsActive);
                _paraList.Add("@CreatedBy", item.CreatedBy);
                _paraList.Add("@ModifiedBy", item.ModifiedBy);

                MyError _error = new MyError();
                Int64 _retValue = _db.Execute("sprocCourseInsertUpdateSingleItem", System.Data.CommandType.StoredProcedure, _paraList, out _error);
                item.Error = _error;

                if (_retValue > 0)
                {
                    item.CourseID = Convert.ToInt32(_retValue);
                    item.Error.ErrorMessage = "Saved successfully!";
                }
                else
                {
                    if (_error.ErrorNo == 0)
                    {
                        item.Error.ErrorNo = 10002;
                        item.Error.ErrorLocation = "Course";
                        item.Error.ErrorMessage = "No row affected.";
                    }
                }

            }
            catch (Exception Ex)
            {
                item.Error.ErrorNo = 10001;
                item.Error.ErrorLocation = "Course";
                item.Error.ErrorMessage = Ex.Message;
                item.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return item;
        }

        private Course FillData(IDataReader aRow, Course item)
        {
            try
            {
                item = new Course();
                item.CourseID = aRow.GetInt32(aRow.GetOrdinal("CourseID"));
                item.SubjectID = aRow.GetInt32(aRow.GetOrdinal("SubjectID"));
                item.GradeLevelID = aRow.GetInt32(aRow.GetOrdinal("GradeLevelID"));
                item.CourseName = aRow.GetString(aRow.GetOrdinal("CourseName"));
                
                if (aRow["ShortName"] != DBNull.Value)
                {
                    item.ShortName = aRow.GetString(aRow.GetOrdinal("ShortName"));
                }
                item.IsActive = aRow.GetBoolean(aRow.GetOrdinal("IsActive"));
                item.CreatedBy = aRow.GetInt32(aRow.GetOrdinal("CreatedBy"));
                if (_db.ColumnExists(aRow, "CreatedByName") && aRow["CreatedByName"] != DBNull.Value)
                {
                    item.CreatedByName = aRow.GetString(aRow.GetOrdinal("CreatedByName"));
                }
                item.CreatedDate = aRow.GetDateTime(aRow.GetOrdinal("CreatedDate"));
                item.ModifiedBy = aRow.GetInt32(aRow.GetOrdinal("ModifiedBy"));
                if (_db.ColumnExists(aRow, "ModifiedByName") && aRow["ModifiedByName"] != DBNull.Value)
                {
                    item.ModifiedByName = aRow.GetString(aRow.GetOrdinal("ModifiedByName"));
                }
                item.ModifiedDate = aRow.GetDateTime(aRow.GetOrdinal("ModifiedDate"));
            }
            catch (Exception Ex)
            {
                item.Error.ErrorNo = 10003;
                item.Error.ErrorLocation = "Course";
                item.Error.ErrorMessage = Ex.Message;
                item.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return item;
        }
    }
}
